<template>
  <div>
    <el-card
      class="fty-card"
      shadow="always"
      style="margin: 36px"
      :body-style="{ padding: '0px' }"
    >
      <template #header>
        <div class="flex justify-between">
            <h2>{{ $t('merchant.information') }}</h2>
            <el-button
              v-if="verifyPermission('126868.MANAGE')"
              type="primary"
              class="w-8"
              @click="handleEdit"
            >
            <svg-icon icon-class="outlined-edit" class="cursor-pointer"/>
            </el-button>
        </div>
      </template>
      <div class="p-4 ">
                <div
                  v-show="merchant.mid"
                  class="flex items-center border-b border-gray-4 py-3"
                > <div class=" text-blue-8 text-xl mr-4" style="line-height: 24px;">
                  <svg-icon icon-class="outlined-shop"></svg-icon>
                </div>
                  <div>
                    <p>MID: {{ merchant.mid }}</p>
                  </div>
                </div>
                <div class="flex items-center border-b border-gray-4 py-3">
                  <div class=" text-blue-8 text-xl mr-4" style="line-height: 24px;">
                  <svg-icon icon-class="outlined-environment"></svg-icon>
                </div>
                  
                  <div>
                    <p>
                      {{ $t('country_or_region.' + merchant.countryCode)
                      }}{{
                        merchant.provName ? '-' + merchant.provName : ''
                      }}
                    </p>
                    <p>{{ merchant.address }}</p>
                  </div>
                </div>

                <div
                  v-show="merchant.contactName"
                  class="flex items-center border-b border-gray-4 py-3"
                >
                <div class=" text-blue-8 text-xl mr-4" style="line-height: 24px;">
                  <svg-icon icon-class="outlined-user"></svg-icon>
                </div>
                  <div>
                    <p>{{ merchant.contactName }}</p>
                  </div>
                </div>

                <div
                  v-show="merchant.email"
                  class="flex items-center border-b border-gray-4 py-3"
                >
                <div class=" text-blue-8 text-xl mr-4" style="line-height: 24px;">
                  <svg-icon icon-class="outlined-mail"></svg-icon>
                </div>
                 
                  <div>
                    <p>{{ merchant.email }}</p>
                  </div>
                </div>

                <div
                  v-show="merchant.mobile"
                  class="flex items-center border-b border-gray-4 py-3"
                >
                <div class=" text-blue-8 text-xl mr-4" style="line-height: 24px;">
                  <svg-icon icon-class="outlined-phone"></svg-icon>
                </div>
                 
                  <div>
                    <p v-show="merchant.mobile">
                      {{ merchant.mobile }}
                    </p>
                  </div>
                </div>
              </div>
      </el-card>
      <el-card
      class="fty-card"
      shadow="always"
      style="margin: 36px"
      :body-style="{ padding: '0px' }"
      :page-size="pageSize"
      :total="rows"
      :current-page="pageNo"
      @current-change="currentChangeHandle"
    >
      <template #header>
        <div class="flex justify-between">
            <h2>{{ $t('merchant.out_bind') }}</h2>
            <el-button
              v-if="verifyPermission('126868.MANAGE')"
              type="primary"
              class="w-8"
              @click="addOutVisible = true"
            >
            <svg-icon icon-class="Plus" class="cursor-pointer"/>
            </el-button>
        </div>
      </template>
      <el-table
        v-loading="listLoading"
        :show-overflow-tooltip="true"
        :data="tableData"
        stripe
      >
      <el-table-column :label="$t('merchant.merchant')" prop="mrchName">

        </el-table-column>
        <el-table-column :label="$t('merchant.out_merchant_no')" prop="mrchNo" align="center"/>
        <el-table-column
          prop="channel"
          align="center"
          min-width="120"
          :label="$t('merchant.channel')"
        ></el-table-column>


        <el-table-column :label="$t('common.create_time')" prop="creTime" width="200">
          <template v-slot="{ row }">
            <div :title="timeFilter(row.creTime)">
              {{ dateFilter(row.creTime) }}
            </div>
          </template>
        </el-table-column>

        <el-table-column :label="$t('common.updateTime')" prop="updTime" width="200">
          <template v-slot="{ row }">
            <div :title="timeFilter(row.updTime)">
              {{ dateFilter(row.updTime) }}
            </div>
          </template>
        </el-table-column>

        <el-table-column width="75" 
        v-if="verifyPermission('126868.MANAGE')"
        >
          <template v-slot="scope">
            <el-dropdown
            >
              <el-button text class="w-8">
                <svg-icon icon-class="MoreFilled" />
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  
                  <el-dropdown-item @click="outMerchantEdit(scope.row)">
                    <svg-icon icon-class="outlined-edit" />
                    {{ $t("common.edit") }}
                  </el-dropdown-item>
                  <el-dropdown-item @click="outerMerchantDelete(scope.row)">
                    <svg-icon icon-class="Delete" />
                    {{ $t("common.delete") }}
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      </el-card>
      <edit-merchant v-model="editVisible" :mrch-id="mrchId" @refresh="refresh" />
      <add-out-merchant v-model="addOutVisible" :mrch-id="mrchId" @refresh="refreshList" />
      <edit-out-merchant v-model="editOutVisible" :mrch-id="mrchId" :out-merchant="currentOutMerchant" @refresh="refreshList" />
  </div>
</template>

<script>
import EditMerchant from '../edit-merchant.vue'
import AddOutMerchant from '../add-out-merchant.vue'
import editOutMerchant from '../edit-out-merchant.vue'
import { timeFilter, dateFilter } from "@/filters";
import { getLinkOuterMerchant, deleteLinkOuterMerchant} from "@/api/merchant"
import { verifyBtnPermission } from "@/utils";

export default {
  components: {
    EditMerchant, AddOutMerchant, editOutMerchant
  },
  props: {
    merchant: {
      type: Object,
      default: ()=> {}
    }
  },
  data() {
    return {
      editVisible: false,
      addOutVisible: false,
      editOutVisible: false,
      currentOutMerchant: {},
      mrchId: this.$route.params.mrchId,
      pageSize: 10,
      rows: 0,
      pageNo: 1,
      tableData: [
      ],
      listLoading: false,
    }
  },
  mounted() {
    this.search()
  },
  methods: {
    timeFilter, dateFilter,

    verifyPermission(val) {
      return verifyBtnPermission(val);
    },
    outMerchantEdit(row) {
      this.currentOutMerchant = row
      this.editOutVisible = true
    },
    outerMerchantDelete(row) {

      this.$confirm(this.$t("merchant.merchant_delete_confirm_tip", { mrchName: row.mrchName }), "", {
        confirmButtonText: this.$t("common.confirm"),
        cancelButtonText: this.$t("common.cancel"),
        type: "warning",
      }).then(() => {
        deleteLinkOuterMerchant([row.id])
          .then((res) => {
            this.$message.success(this.$t("common.operate_success"));
            this.search();
          })
          .catch((e) => {
            this.$confirmError(e);
          });
      });

    },
    handleEdit(){
      console.log('edit');
      this.editVisible = true
    },
    refresh() {
      this.$emit('refresh')
    },
    refreshList() {
      this.pageNo = 1,
      this.search()
    },
    search(){
      this.listLoading = true
      getLinkOuterMerchant(this.mrchId, {
        pageNo: this.pageNo,
        pageSize: this.pageSize
      }).then((res)=>{
        this.rows = res.rows
        this.tableData = res.data
      this.listLoading = false
      }).catch((error)=>{
      this.listLoading = false
        this.$confirmError(error)
      })
    },
    currentChangeHandle(val) {
      this.pageNo = val;
      this.search();
    },
  }
}

</script>

<style lang="scss" scoped>

</style>
